package com.ding.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageService {

	@Autowired
	RabbitTemplate rabbit;
	/*
	 * convertAndSend("交换机Id"," routingKey", 消息):经过交换机,由交换机路由消息
	 * convertAndSend("队列Id", 消息):直接将消息发往队列
	 */
	
	public void send_Fanout(String msg) {
		rabbit.convertAndSend("fanout","", msg);
	}
	public void sent_Direct_By_Exchange(String routingKey,String msg) {
		rabbit.convertAndSend("direct",routingKey, msg);
	}
	public void sent_Direct_No_Exchange(String queue,String msg) {
		rabbit.convertAndSend(queue, msg);
	}
	public void sent_topic(String routingKey,String msg) {
		rabbit.convertAndSend("topic",routingKey , msg);
	}


	//发送延时消息
	public void sendTest()
	{
    	System.out.println(System.currentTimeMillis());
        rabbit.convertAndSend("fq", new T(),(message)->{
			//消息超时时间应该小于缓冲队列的x-message-ttl属性
			message.getMessageProperties().setExpiration(Long.toString(1000L));
            return message;
		};
	}


}
